package com.kgar.entity.pojo;

import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.*;
import org.springframework.data.elasticsearch.core.suggest.Completion;
import java.util.ArrayList;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "job_info")
@Setting(settingPath = "/elasticsearch/settings.json")
public class JobInfoDoc {
    @Id
    private String jobId;
    @Field(type = FieldType.Text,analyzer = "text_analyzer",searchAnalyzer = "ik_smart",copyTo = "recommend")
    private String company;
    @Field(type = FieldType.Text,analyzer = "text_analyzer",searchAnalyzer = "ik_smart",copyTo = "recommend")
    private String title;
    @Field(type = FieldType.Keyword,index = false)
    private String salary;
    @Field(type = FieldType.Keyword,copyTo = "recommend")
    private String education;
    @Field(type = FieldType.Text,analyzer = "text_analyzer",searchAnalyzer = "ik_smart",copyTo = "recommend")
    private String description;
    @Field(type = FieldType.Keyword,index = false)
    private String hiringManager;
    @Field(type = FieldType.Keyword)
    private String lastActive;
    @Field(type = FieldType.Text,analyzer = "text_analyzer",searchAnalyzer = "ik_smart",copyTo = "recommend")
    private String address;
    @Field(type = FieldType.Keyword,index = false)
    private String link;

    @Field(type = FieldType.Text,analyzer = "text_analyzer",searchAnalyzer = "ik_smart")
    private String recommend;

    @Field(type = FieldType.Keyword,copyTo = "recommend")
    private List<String> tags;

    @CompletionField(analyzer = "completion_analyzer")
    private Completion suggestion;

    public JobInfoDoc(JobInfo jobInfo){

        jobId = jobInfo.getJobId().toString();
        company = jobInfo.getCompany();
        title = jobInfo.getTitle();
        salary = jobInfo.getSalary();
        education = jobInfo.getEducation();
        description = jobInfo.getDescription();
        hiringManager = jobInfo.getHiringManager();
        lastActive = jobInfo.getLastActive();
        address = jobInfo.getAddress();
        link = jobInfo.getLink();

        tags = new ArrayList<>(JSONUtil.toList(jobInfo.getTags(), String.class));

        suggestion = new Completion();
        suggestion.setInput(new String[]{jobInfo.getCompany(), jobInfo.getTitle(), jobInfo.getEducation(), jobInfo.getAddress()});


    }
}
